const ipUtil = require("../../utils/hostUtil")
const requestUtil = require("../../utils/requestUtil")
Page({
    data: {
        address: "",
        enableConfirm: false,
        enableCancel: false,
        countdown: {
            day: '',
            hour: '',
            minute: '',
            second: ''
        }
    },
    onLoad: function (options) {
        var orderId = options.orderId;
        this.setData({
            orderId: orderId
        })
        // 加载页面数据
        this.loadPage()
    },
    onShow: function () {
        this.loadPage();
    },
    onHide:function (){
        if (this.data.interval != "") {
            clearInterval(this.data.interval);
        }
    },
    onUnload:function (){
        if (this.data.interval != "") {
            clearInterval(this.data.interval);
        }
    },
    /**
     * 复制订单号
     * @param e
     */
    copytext: function (e) {
        var text = e.currentTarget.dataset.text;
        wx.setClipboardData({
            data: e.currentTarget.dataset.text,
            success: function (res) {
                wx.getClipboardData({
                    success: function (res) {
                        wx.showToast({
                            title: '复制成功'
                        })
                    }
                })
            }
        })
    },
    startCountdown: function (serverTime, endTime) {
        var that = this;
        serverTime = new Date(serverTime);
        var millisecond = endTime.getTime() - serverTime.getTime();

        var interval = setInterval(function () {
            millisecond -= 1000;
            if (millisecond <= 0) {
                clearInterval(interval);
                that.setData({
                    countdown: {
                        day: '00',
                        hour: '00',
                        minute: '00',
                        second: '00'
                    }
                });
                return;
            }
            that.transformRemainTime(millisecond);
        }, 1000);
    },
    // 剩余时间(毫秒)处理转换时间
    transformRemainTime: function (millisecond) {
        var that = this;
        var countdownObj = that.data.countdown;
        // 秒数
        var seconds = Math.floor(millisecond / 1000);
        // 天数
        countdownObj.day = that.formatTime(Math.floor(seconds / 3600 / 24));
        // 小时
        countdownObj.hour = that.formatTime(Math.floor(seconds / 3600 % 24));
        // 分钟
        countdownObj.minute = that.formatTime(Math.floor(seconds / 60 % 60));
        // 秒
        countdownObj.second = that.formatTime(Math.floor(seconds % 60));
        that.setData({
            countdown: countdownObj
        });
    },
    //格式化时间为2位
    formatTime: function (time) {
        if (time < 10)
            return '0' + time;
        return time;
    },
    loadPage: function () {
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.order_detail}`,
            data: {orderId: this.data.orderId},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                let order = res.data.data;
                let startTime = new Date();
                let endTime = new Date(order.firstime);
                endTime.setMinutes(endTime.getMinutes() + 15);
                if (order.state == 0) {
                    that.startCountdown(startTime, endTime);
                }
                that.setData({
                    order: order,
                    orderId: res.data.data.orderId,
                    money: res.data.data.money,
                    address: res.data.data.address,
                    state: order.state
                })
            }
        })
    }
    , pay: function () {
        let that = this;
        let orderId = this.data.orderId;
		let couponid = wx.getStorageSync('couponid')
        wx.request({
            url: `${ipUtil.host}${requestUtil.pay}`,
            data: {orderId: orderId,evaluationId:couponid},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.code == 0) {
                    let data = res.data.data;
                    wx.requestPayment({
                        appId: data.appId,
                        timeStamp: data.timeStamp,
                        nonceStr: data.nonceStr,
                        package: data.package,
                        signType: "MD5",
                        paySign: data.sign,
                        success(res1) {
                            that.data.interval = setInterval(function () {
                                wx.request({
                                    url: `${ipUtil.host}${requestUtil.pay_result}`,
                                    data: {orderId: orderId,evaluationId:couponid},
                                    method: "POST",
                                    header: requestUtil.header,
                                    success(res2) {
                                        if (res2.data.data == 1) {
                                            clearInterval(that.data.interval);
                                            wx.showToast({
                                                title: "付款成功",
                                                icon: "success",
                                                duration: 2000
                                            })
                                            that.loadPage();
                                        } else if (res2.data.data == -1) {
                                            clearInterval(that.data.interval);
                                            wx.showToast({
                                                title: "订单已取消",
                                                icon: "success",
                                                duration: 2000
                                            })
                                            that.loadPage();
                                        }
                                    }
                                });
                            }, 3000)
                        }, fail(res) {
                            wx.showToast({
                                title: "取消支付",
                                icon: "none",
                                duration: 2000
                            })
                            that.loadPage();
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none",
                        duration: 2000
                    })
                }
            }
        })
    },
    /**
     * cancell order
     */
    cancelOrder: function () {
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.order_cancell}`,
            data: {orderId: that.data.orderId},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.code == 0) {
                    wx.showToast({
                        title: "取消成功",
                        icon: "success",
                        duration: 2000
                    })
                    // wx.navigateBack({
                    //     delta: 1
                    // })
                    that.loadPage();
                }
            }
        })
    },
    /**
     * 查看物流
     */
    ship_state: function () {
        wx.showToast({
            title: "功能正在开发...",
            icon: "none",
            duration: 2000
        })
    }, confirmOrder: function () {
        var that = this;
        wx.request({
            url: `${ipUtil.host}${requestUtil.confirm}`,
            data: {orderId: that.data.orderId},
            method: "POST",
            header: requestUtil.header,
            success(res) {
                if (res.data.code == 0) {
                    wx.showToast({
                        title: "收货成功",
                        icon: "none",
                        duration: 2000
                    })
                    that.loadPage();
                }
            }
        })
    },
    rebuy: function () {
        wx.showToast({title: '功能正在开发...', icon: 'none', duration: 2000})
        return;
    }
});